#include "BoyerMore.h"

char *boyerMore(const char *pattern, const char *text, int txtLen)
{
	int i=0;
	
	if(text==null||pattern==null||txtLen<=0)
		return null;
	
	int patternLen = strlen(pattern);
	
	char charJump[ALPHABETSIZE];
	
	for(i=0;i<ALPHABETSIZE;i++)
		charJump[i]=patternLen;

	//init charJump array
	for(i=0;i<patternLen;i++)
		charJump[(unsigned char)pattern[i]] = patternLen-i-1;

	int txtPosition = patternLen-1;
	int patPosition = patternLen;
	
	//now search
	while(txtPosition<=txtLen&&patPosition!=0)
	{
		if(pattern[patPosition-1]==text[txtPosition])
		{
			patPosition--;
			txtPosition--;
		}
		else
		{
			txtPosition+=charJump[text[txtPosition]];
			patPosition=patternLen;
		}
	}

	if(patPosition==0)
		return (char *)(text+txtPosition);
	else 
		return null;
}

